package com.team.base.module.controller.sys.order;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.team.base.base.entity.ResponseResult;
import com.team.base.base.interceptor.UserLoginToken;
import com.team.base.base.page.Page;
import com.team.base.common.export.DownExcle;
import com.team.base.common.export.ExcelListener;
import com.team.base.module.entity.sys.order.Order;
import com.team.base.module.service.sys.order.OrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Api(tags = { "订单管理" })
@RestController
@RequestMapping(value = "/auth/order/")
public class OrderController {

	@Resource
	private OrderService orderService;

	@UserLoginToken
	@GetMapping(value = "get/{id}")
	@ApiOperation(value = "查询订单信息")
	public ResponseResult<Order> get(@PathVariable(value = "id") String id) {
		ResponseResult<Order> result = new ResponseResult<Order>();
		Order order = this.orderService.get(id);
		result.setData(order);
		return result;
	}

	@UserLoginToken
	@GetMapping(value = "list")
	@ApiOperation(value = "查询订单分页列表")
	public ResponseResult<Page<Order>> list(Order order, @RequestParam(value = "pageNum", required = true) int pageNum,
			@RequestParam(value = "pageSize", required = true) int pageSize) {
		ResponseResult<Page<Order>> result = new ResponseResult<Page<Order>>();
		Page<Order> page = this.orderService.findPage(new Page<Order>(pageNum, pageSize), order);
		result.setData(page);
		return result;
	}

	@UserLoginToken
	@PostMapping(value = "import")
	@ApiOperation(value = "导入订单信息")
	public ResponseResult<Boolean> importexcel(MultipartFile file) throws IOException {
		ResponseResult<Boolean> result = new ResponseResult<Boolean>();
		try {
			EasyExcel.read(file.getInputStream(), Order.class, new ExcelListener(orderService)).sheet().doRead();
			log.info("importData finished");
			result.setData(true);
		} catch (IOException e) {
			log.info("导入订单信息失败");
			e.printStackTrace();
		}
		return result;
	}

	@UserLoginToken
	@PostMapping(value = "export")
	@ApiOperation(value = "导出订单信息")
	public ResponseResult<Boolean> export(HttpServletResponse response) {
		ResponseResult<Boolean> result = new ResponseResult<Boolean>();
		try {
			Order order = new Order();
			// 到时候会根据用户信息查询订单，现在是查询所有
			List<Order> orderLists = this.orderService.findList(order);

			// 导出订单信息
			DownExcle.download(response, Order.class, orderLists, "订单信息");
			result.setData(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}